import { Module } from 'vuex'
import { ObjectString } from '@/types/type-common'
import { setStorage } from '@/utils/auth'

interface State {
  showSide: boolean
  routeTabs: ObjectString[]
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const app: Module<any, State> = {
  state: {
    showSide: true,
    routeTabs: []
  },
  mutations: {
    UPDATE_SHOW_SIDE(state, val) {
      state.showSide = val
    },
    ADD_ROUTE_TAB(state, val) {
      state.routeTabs.forEach((item: { isActive: string }) => {
        item.isActive = 'no'
      })
      const item = state.routeTabs.find((item: { name: string }) => item.name === val.name)
      if (item) {
        item.isActive = 'yes'
      } else {
        state.routeTabs.push(val)
      }
    },
    DEL_ROUTE_TAB(state, val) {
      const index = state.routeTabs.findIndex((item: { name: string }) => item.name === val)
      state.routeTabs.splice(index, 1)
    }
  },
  actions: {
    updateShowSide({ commit }, data: boolean) {
      commit('UPDATE_SHOW_SIDE', data)
      setStorage('showSide', data === false ? 'false' : 'true')
    },
    addRouteTab({ commit }, data: ObjectString) {
      data.isActive = 'yes'
      commit('ADD_ROUTE_TAB', data)
    },
    delRouteTab({ commit }, data: string) {
      commit('DEL_ROUTE_TAB', data)
    }
  }
}

export { app, State }
export default app
